package main.剑指OfferⅠ;

import java.util.HashMap;
import java.util.Map;

public class Day26 {
    public static void main(String[] args) {//AC自动机
        System.out.println("剑指Offer20.表示数值的字符串");
        System.out.println("剑指Offer67.把字符串转换成整数");
    }
}

class Offer20{
    public boolean isNumber(String s) {
        Map[] states = {
                new HashMap<Character,Integer>() {{ put(' ', 0); put('s', 1); put('d', 2); put('.', 4); }}, // 0.
                new HashMap<Character,Integer>() {{ put('d', 2); put('.', 4); }},                           // 1.
                new HashMap<Character,Integer>() {{ put('d', 2); put('.', 3); put('e', 5); put(' ', 8); }}, // 2.
                new HashMap<Character,Integer>() {{ put('d', 3); put('e', 5); put(' ', 8); }},              // 3.
                new HashMap<Character,Integer>() {{ put('d', 3); }},                                        // 4.
                new HashMap<Character,Integer>() {{ put('s', 6); put('d', 7); }},                           // 5.
                new HashMap<Character,Integer>() {{ put('d', 7); }},                                        // 6.
                new HashMap<Character,Integer>() {{ put('d', 7); put(' ', 8); }},                           // 7.
                new HashMap<Character,Integer>() {{ put(' ', 8); }}                                         // 8.
        };
        int p = 0;
        char t;
        for(char c : s.toCharArray()) {
            if(c >= '0' && c <= '9') t = 'd';
            else if(c == '+' || c == '-') t = 's';
            else if(c == 'e' || c == 'E') t = 'e';
            else if(c == '.' || c == ' ') t = c;
            else t = '?';
            if(!states[p].containsKey(t)) return false;
            p = (int)states[p].get(t);
        }
        return p == 2 || p == 3 || p == 7 || p == 8;
    }
}

class Offer67{
    public int strToInt(String str) {
        char[] s=str.toCharArray();
        StringBuffer result=new StringBuffer();
        int i=0;//第一个非空字符位置
        char flag='*';
        while (i<s.length&&s[i]==' ') i++;
        if (i==s.length) return 0;//全空
        if (s[i]=='-'||s[i]=='+'){
            flag=s[i];
            i++;
            while (i<s.length&&(s[i]>='0'&&s[i]<='9')){
                result.append(s[i]);
                i++;
            }
            if (result.length()==0) return 0;
        }else if (s[i]>='0'&&s[i]<='9'){
            while (i<s.length&&(s[i]>='0'&&s[i]<='9')){
                result.append(s[i]);
                i++;
            }
        }else return 0;
        StringBuffer temp=new StringBuffer();
        int j=0;
        while (j<result.length()&&result.charAt(j)=='0') j++;
        while (j<result.length()) {
            temp.append(result.charAt(j));
            j++;
        }
        if (temp.length()==0) return 0;
        String ans=null;
        if (flag=='-') ans=flag+temp.toString();
        else ans=temp.toString();
        long num= 0;
        try {
            num = Long.valueOf(ans);
        } catch (NumberFormatException e) {
            if (flag=='-') return Integer.MIN_VALUE;
            else return Integer.MAX_VALUE;
        }
        if (num>Integer.MAX_VALUE) return Integer.MAX_VALUE;
        if (num<Integer.MIN_VALUE) return Integer.MIN_VALUE;
        return (int) num;
    }
}